﻿Imports System.Collections.Specialized

Public Class frmTabVectorInformation
    Dim editnode As EditXML = New EditXML()
    Public Shared sPdelim As String = "£"
    Public Shared sSDTS As StringCollection = New StringCollection()  'holds collection of SDTS or VPF terms descriptions, if used

    Private Sub frmTabVectorInformation_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        editnode.Remove("/metadata/spdoinfo", "rastinfo")
        editnode.Remove("/metadata/spdoinfo", "ptvctinf")
        If frmMain.f31.cboxDirect.SelectedItem = "Point" Or frmMain.f31.cboxDirect.SelectedItem = "Vector" Then
            Dim newNode As String = ""
            If rdoSDTS.Checked Then
                For i As Integer = 1 To sSDTS.Count()
                    Dim items() As String = sSDTS(i - 1).Split(sPdelim)
                    newNode = newNode & "<sdtsterm><sdtstype>" & items(0) & "</sdtstype>"
                    If items.GetUpperBound(0) = 1 Then newNode = newNode & "<ptvctcnt>" & items(1) & "</ptvctcnt>"
                    newNode = newNode & "</sdtsterm>"
                Next
                editnode.Add("/metadata/spdoinfo", "/direct", "ptvctinf", newNode)
            ElseIf rdoVPF.Checked Then
                newNode = newNode & "<vpfterm><vpflevel>" & cboxVPF.SelectedItem & "</vpflevel>"
                For i As Integer = 1 To sSDTS.Count()
                    Dim items() As String = sSDTS(i - 1).Split(sPdelim)
                    newNode = newNode & "<vpfinfo><vpftype>" & items(0) & "</vpftype>"
                    If items.GetUpperBound(0) = 1 Then newNode = newNode & "<ptvctcnt>" & items(1) & "</ptvctcnt>"
                    newNode = newNode & "</vpfinfo>"
                Next
                newNode = newNode & "</vpfterm>"
                editnode.Add("/metadata/spdoinfo", "/direct", "ptvctinf", newNode)
            End If
        End If
    End Sub
    Private Sub btnAddTerm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTerm.Click
        Dim sObjCount As String
        sObjCount = "" & sPdelim & "" 'blank object type and blank count
        Dim bObjType As Boolean
        If (rdoSDTS.Checked = True) Then
            bObjType = True
        Else
            bObjType = False
        End If
        Dim frmAddTerm As New frmTermsDesc(sObjCount, bObjType)
        frmAddTerm.ShowDialog()
        If (frmAddTerm.getUpdated() = False) Then Return 'no object to be added
        sObjCount = frmAddTerm.getTerm()
        sSDTS.Add(sObjCount)
        sObjCount = Replace(sObjCount, sPdelim, "  :  ")
        lboxTerms.Items.Add(sObjCount)
    End Sub

    Private Sub btnEditTerm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditTerm.Click
        Dim iCount As Integer = lboxTerms.Items.Count
        Dim iSelInd As Integer = lboxTerms.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then 'no term description selected
            MessageBox.Show("No description selected for editing.", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        'if valid description selected for editing, then process
        Dim sObjCount As String
        sObjCount = sSDTS(iSelInd) 'get the selected object type information
        Dim bObjType As Boolean
        If (rdoSDTS.Checked = True) Then
            bObjType = True
        Else
            bObjType = False
        End If
        Dim frmEditTerm As New frmTermsDesc(sObjCount, bObjType)
        frmEditTerm.ShowDialog()
        If (frmEditTerm.getUpdated() = False) Then Return 'no object to be added
        sObjCount = frmEditTerm.getTerm()
        sSDTS.RemoveAt(iSelInd) 'remove old version
        sSDTS.Insert(iSelInd, sObjCount)   'insert new version
        sObjCount = Replace(sObjCount, sPdelim, "  :  ")
        lboxTerms.Items.RemoveAt(iSelInd)
        lboxTerms.Items.Insert(iSelInd, sObjCount)
        btnEditTerm.Enabled = False
        btnDelTerm.Enabled = False
    End Sub

    Private Sub btnDelTerm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelTerm.Click
        Dim iSelInd As Integer = lboxTerms.SelectedIndex
        If (iSelInd = -1) Then 'no term description selected
            MessageBox.Show("No description selected for deletion.", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        sSDTS.RemoveAt(iSelInd) 'string collection has zero-based index
        lboxTerms.Items.RemoveAt(iSelInd) 'list box has zero-based index
        btnEditTerm.Enabled = False
        btnDelTerm.Enabled = False
    End Sub

    Private Sub lboxTerms_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxTerms.SelectedIndexChanged
        If lboxTerms.SelectedItems.Count() > 0 Then btnEditTerm.Enabled = True : btnDelTerm.Enabled = True Else btnEditTerm.Enabled = False : btnDelTerm.Enabled = False
    End Sub

    Private Sub rdoSDTS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoSDTS.CheckedChanged
        Panel14.Visible = True
        Panel15.Visible = False
        lblSDTS.Text = "SDTS Terms Description"
    End Sub

    Private Sub rdoVPF_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoVPF.CheckedChanged
        Panel14.Visible = True
        Panel15.Visible = True
        lblSDTS.Text = "VPF Terms Description"
    End Sub

    Private Sub rdoNoDesc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoNoDesc.CheckedChanged
        Panel14.Visible = False
        Panel15.Visible = False
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("VectorInformation")
        helpForm.ShowDialog()
    End Sub
End Class